package com.woniu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.mapper.OrganizationMapper;
import com.woniu.mapper.RoleMapper;
import com.woniu.mapper.TeamMapper;
import com.woniu.pojo.Doctor;
import com.woniu.mapper.DoctorMapper;
import com.woniu.pojo.Organization;
import com.woniu.pojo.Role;
import com.woniu.pojo.Team;
import com.woniu.service.IDoctorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.DoctorDetailVo;
import com.woniu.vo.DoctorVo;
import com.woniu.vo.QuerySearchDoctorVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author doujiao
 * @since 2022-10-10
 */
@Service
@Slf4j
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements IDoctorService {
    @Resource
    private DoctorMapper doctorMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private TeamMapper teamMapper;
    @Resource
    private OrganizationMapper organizationMapper;
    /**
     * 分页搜索查找所有医生
     * @param currentPageStr
     * @param pageSizeStr
     * @param searchDoctorVo
     * @return
     */
    public PageResult searchByPage(String currentPageStr, String pageSizeStr, QuerySearchDoctorVo searchDoctorVo) {
        //转Integer
          Integer currentPage = Integer.parseInt(currentPageStr);
          Integer pageSize = Integer.parseInt(pageSizeStr);
        //分页
        IPage<Doctor> page =new Page(currentPage,pageSize);
        PageResult pageResult = new PageResult();
        //传过来的对象不是空的
        if (searchDoctorVo!=null){
            //构造wrap
            QueryWrapper<Doctor> wrapper=new QueryWrapper();
            //机构不为空
            if (searchDoctorVo.getOrganizationId()!=null){
              wrapper.eq("organization_id",searchDoctorVo.getOrganizationId());
            }
            //团队
            if (searchDoctorVo.getTeamId()!=null){
                wrapper.eq("team_id",searchDoctorVo.getTeamId());
            }
            //标签
            if (searchDoctorVo.getLabelId()!=null){
                wrapper.eq("role_id",searchDoctorVo.getLabelId());
            }
            //名称
            if (searchDoctorVo.getName()!=null){
                wrapper.like("name",searchDoctorVo.getName());
            }
            IPage<Doctor> doctorIPage = doctorMapper.selectPage(page, wrapper);
            List<Doctor> doctorList = doctorIPage.getRecords();
            List<DoctorVo> doctorVoList = new ArrayList<>();
            //遍历doctorlist
            for (Doctor doctor : doctorList) {
                DoctorVo doctorVo = new DoctorVo();
                //复制
                BeanUtils.copyProperties(doctor,doctorVo);
                //根据role_id查询role
                Role role = roleMapper.selectById(doctor.getRoleId());
                doctorVo.setRole(role.getName());
                //根据teamId查询team
                Team team = teamMapper.selectById(doctor.getTeamId());
                doctorVo.setTeam(team.getName());
                //根据organizationId查询organization
                Organization organization = organizationMapper.selectById(doctor.getOrganizationId());
                doctorVo.setOrganization(organization.getName());
                doctorVoList.add(doctorVo);
            }
            pageResult.setTotal(doctorIPage.getTotal());
            pageResult.setData(doctorVoList);
            return pageResult;
        }
        IPage<Doctor> doctorIPage = doctorMapper.selectPage(page, null);
        log.debug(String.valueOf(doctorIPage.getTotal()));
         pageResult.setTotal(doctorIPage.getTotal());
         pageResult.setData(doctorIPage.getRecords());
         return pageResult;
    }

    @Override
    public List<Doctor> getByTeamId(Integer id) {

        List<Doctor>  doctorList = doctorMapper.selectByTeamId(id);

        return doctorList;
    }




    /**
     * 根据id查看详情
     * @param id
     * @return
     */
    public DoctorDetailVo queryDetailById(Long id) {
        Doctor doctor = doctorMapper.selectById(id);
        DoctorDetailVo doctorDetailVo = new DoctorDetailVo();
        //复制
        BeanUtils.copyProperties(doctor,doctorDetailVo);
        Long organizationId = doctor.getOrganizationId();
        //根据机构id查询
        Organization organization = organizationMapper.selectById(organizationId);
        //根据团队id查询
        Long teamId = doctor.getTeamId();
        Team team = teamMapper.selectById(teamId);
        //根据role_id查询
        Long roleId = doctor.getRoleId();
        Role role = roleMapper.selectById(roleId);
        String teamName=organization.getName()+"/"+team.getName()+"/"+role.getName();
        doctorDetailVo.setTeamName(teamName);
        return doctorDetailVo;
    }
}
